﻿using AutoMapper;
using AutoMapper.QueryableExtensions;
using com.ytocargo.Application.FlightInfos.Dto;
using com.ytocargo.Core.FlightInfos;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yto.AutoMapper.Extensions;
using Yto.Common.Configuration;
using Yto.Common.Dependency;
using Yto.Unitwork;

namespace com.ytocargo.Application.FlightInfos
{
    public class FlightInfosServices : IFlightInfosServices, ITransient
    {
        private readonly ICtnManager _ctnManager;
        private readonly IFlightInfoStore _flightInfoStore;
        private readonly IMapper _mapper;
        private readonly IStartupConfiguration _startupConfiguration;

        public FlightInfosServices(ICtnManager ctnManager,
            IFlightInfoStore flightInfoStore,
            IMapper mapper,
            IStartupConfiguration startupConfiguration
            )
        {
            _ctnManager = ctnManager;
            _flightInfoStore = flightInfoStore;
            _mapper = mapper;
            _startupConfiguration = startupConfiguration;
        }

        public List<FlightInfoDto> GetTomorrowFlightInfos(FlightInfoFilter filter)
        {
            var list = _flightInfoStore.GetAll()
                .Where(x => x.FlightDate == filter.FlightDate)
                .ProjectTo<FlightInfoDto>(_mapper.ConfigurationProvider)
                .ToList();
            return list;
        }

        [Unitwork]
        public Task<List<FlightInfoDto>> GetTomorrowFlightInfosAsync(FlightInfoFilter filter)
        {
            var list = _mapper.Map<List<FlightInfoDto>>(GetTomorrowFlightInfos(filter));
            return Task.FromResult(list);
        }
    }
}
